<?php

namespace app\admin\controller;

use app\admin\model\SysConfig as SysConfig_model;
use app\common\Cache\SystemConfigCache;

class SysConfig extends AdminBase
{
    public function getList()
    {
        $data = SysConfig_model::getInstance()->getList(15);
        $this->assign("data", $data['data']);
        $this->assign("domain", $this->domain);
        $this->assign("page", $data['page']);
        return $this->fetch();
    }

    /**
     * @return mixed|void
     */
    public function add()
    {
        if (request()->isPost()) {
            $input = input();
            //dump($input);exit;
            if (empty($input['name'])) {
                $this->error('配置名不可为空');
            }
            if (empty($input['value'])) {
                $this->error('配置值不可为空');
            }
            if (empty($input['describe'])) {
                $this->error('描述不可为空');
            }
            $insert_data = array(
                'name' => strtoupper($input['name']),
                'value' => $input['value'],
                'describe' => $input['describe'],
                'status' => $input['status']
            );
            $res = SysConfig_model::getInstance()->insert($insert_data);
            if ($res > 0) {
                SystemConfigCache::delConfigCache($insert_data['name']);
                $this->success('添加成功', 'getList');
            } else {
                $this->error('添加失败，请重试');
            }
        } else {
            //加载页面
            return $this->fetch();
        }
    }

    /**
     * @return mixed|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function update()
    {
        if (request()->isPost()) {
            $input = input();
            if (empty($input['name'])) {
                $this->error('配置名不可为空');
            }
            if (empty($input['value'])) {
                $this->error('配置值不可为空');
            }
            if (empty($input['describe'])) {
                $this->error('描述不可为空');
            }
            $insert_data = array(
                'name' => strtoupper($input['name']),
                'value' => $input['value'],
                'describe' => $input['describe'],
                'status' => $input['status']
            );
            $res = SysConfig_model::getInstance()->where(array('id' => $input['id']))->update($insert_data);
            if ($res > 0) {
                SystemConfigCache::delConfigCache($insert_data['name']);
                $this->success('修改成功', 'getList');
            } else {
                $this->error('添加失败，请重试');
            }
        } else {
            $coin_data = SysConfig_model::getInstance()->getOne(array('id' => input('id')));
            $this->assign('data', $coin_data);
            $this->assign('domain', $this->domain);
            //加载页面
            return $this->fetch();
        }
    }

    public function del()
    {
        $id = intval(input('id'));
        $data = SysConfig_model::getInstance()->getOne($id);
        if (!$data) {
            $this->error('配置不存在');
        }
        $res = SysConfig_model::getInstance()->delConfig($id);
        if ($res > 0) {
            SystemConfigCache::delConfigCache($data['name']);
            $this->success('删除成功', 'getList');
        } else {
            $this->error('删除失败，请重试');
        }
    }

}
